<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- base href="http://www.codeslinger.co.uk/pages/projects/gameboy/opcodes.html" --></head><body><div style="background:#fff;border:1px solid #999;margin:-1px -1px 0;padding:0;"><div style="background:#ddd;border:1px solid #999;color:#000;font:13px arial,sans-serif;font-weight:normal;margin:12px;padding:8px;text-align:left">Esta es la versión en caché de <a href="http://www.codeslinger.co.uk/pages/projects/gameboy/opcodes.html" style="text-decoration: underline; color: rgb(0, 0, 204);">http://www.codeslinger.co.uk/pages/projects/gameboy/opcodes.html</a>
 de Google. Se trata de una captura de pantalla de la página tal como 
esta se mostraba el 19 Jun 2011 11:08:22 GMT. Es posible que la <a href="http://www.codeslinger.co.uk/pages/projects/gameboy/opcodes.html" style="text-decoration: underline; color: rgb(0, 0, 204);">página</a> haya sufrido modificaciones durante este tiempo. <a href="http://www.google.com/intl/es/help/features_list.html#cached" style="text-decoration: underline; color: rgb(0, 0, 204);">Más información</a><br><br><div style="float:right"><a href="http://webcache.googleusercontent.com/search?q=cache:hNPJTa7xrnMJ:www.codeslinger.co.uk/pages/projects/gameboy/opcodes.html+codeslinger+gameboy+opcode+examples&amp;hl=es&amp;gl=ar&amp;strip=1" style="text-decoration: underline; color: rgb(0, 0, 204);">Versión de solo texto</a></div>
<div>Se han resaltado estos términos de búsqueda: <span style="background:#ffff66;color:black;font-weight:bold">codeslinger</span>&nbsp;<span style="background:#a0ffff;color:black;font-weight:bold">gameboy</span>&nbsp;<span style="background:#99ff99;color:black;font-weight:bold">opcode</span>&nbsp;<span style="background:#ff9999;color:black;font-weight:bold">examples</span>&nbsp;&nbsp;</div></div></div><div style="position:relative">



<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="description" content="_Emulation Programming_">
<meta name="keywords" content="_Emulation,Chip8,Gameboy,NES,SNES,Genesis,Master,System,Mega,Drive_">
<meta name="author" content="CodeSlinger /">
<link rel="stylesheet" type="text/css" href="11%20Opcode%20examples_files/css.css" media="screen,projection" title="CodeSlinger (screen)">
<link rel="stylesheet" type="text/css" href="11%20Opcode%20examples_files/print.css" media="print">
<title>codeslinger.co.uk</title>



<div>
<br><br>
</div>

<div id="container">
<div id="logo">
<h1><a href="http://www.codeslinger.co.uk/index.html"><b style="color:black;background-color:#ffff66">codeslinger</b>.co.uk</a></h1>
</div>

<div id="navitabs">
<h2 class="hide">Site menu:</h2>
<a class="navitab" href="http://www.codeslinger.co.uk/index.php">Home</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/basics.html">Basics</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/projects/zuko.html">Zuko</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/projects/megadrive.html">Mega Drive/Genesis</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/projects/mastersystem.html">Master System</a><span class="hide"> | </span>
<a class="activenavitab" href="http://www.codeslinger.co.uk/pages/projects/gameboy.html"><b style="color:black;background-color:#a0ffff">Gameboy</b></a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/projects/chip8.html">Chip8</a><span class="hide"> | </span>
<a class="navitab" href="http://www.codeslinger.co.uk/pages/blog/index.html">Blog</a><span class="hide"> | </span>
</div>
	
<div id="desc">
<h2><b style="color:black;background-color:#ffff66">codeslinger</b>.co.uk</h2>
<p> <b style="color:black;background-color:#a0ffff">Gameboy</b> - Example Opcodes.</p>

</div>

<div id="main">

<h3><b>ExecuteNextOpcode:</b></h3>
<p>
So far this tutorial has focussed on how the internals of the <b style="color:black;background-color:#a0ffff">gameboy</b>
 fit together and how to emulate each. I often mentioned how knowing how
 long each instruction takes to execute on the original
hardware we can synchronize the graphics and timers. This information 
was acquired in the main update loop from the function called 
ExecuteNextOpcode, however I never gave an implementation to this 
function,
mainly because it was to do with emulating the Z80 like processor, and I
 was leaving this to last. As I have discussed all other aspects of the <b style="color:black;background-color:#a0ffff">gameboy</b> I can now give the definition of this function.

</p><p class="code">

int Emulator::ExecuteNextOpcode( )<br>
{<br>
&nbsp;&nbsp; int res = 0 <br>
&nbsp;&nbsp; BYTE <b style="color:black;background-color:#99ff99">opcode</b> = ReadMemory(m_ProgramCounter) ; <br>
&nbsp;&nbsp; m_ProgramCounter++ ;<br>
&nbsp;&nbsp; res = ExecuteOpcode(<b style="color:black;background-color:#99ff99">opcode</b>) ; <br>
&nbsp;&nbsp; return res ;<br> 
}

</p>

Well thats short and sweet, however now we have to implement the 
ExecuteOpcode function which is a big switch statement. There are 
approximately 300 opcodes for the <b style="color:black;background-color:#a0ffff">gameboy</b>
 cpu so to show the entire
switch statement on this page would be ridiculous, so instead of showing
 all of them I shall just show the ones that I will give <b style="color:black;background-color:#ff9999">examples</b> to on this page.

<p class="code">
int Emulator::ExecuteOpcode(BYTE <b style="color:black;background-color:#99ff99">opcode</b>)<br>
{ <br>
&nbsp;&nbsp; switch(<b style="color:black;background-color:#99ff99">opcode</b>)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp; case 0x06: <br>
&nbsp;&nbsp; CPU_8BIT_LOAD(m_RegisterBC.hi) ; <br>
&nbsp;&nbsp; return 8;<br><br>

&nbsp;&nbsp; case 0x80: <br>
&nbsp;&nbsp; CPU_8BIT_ADD(m_RegisterAF.hi, m_RegisterBC.hi,false,false) ; <br>
&nbsp;&nbsp; return 4;<br><br>

&nbsp;&nbsp; case 0x90: <br>
&nbsp;&nbsp; CPU_8BIT_SUB(m_RegisterAF.hi, m_RegisterBC.hi,false,false) ; <br>
&nbsp;&nbsp; return 4 ;<br><br>

&nbsp;&nbsp; case 0xAF: <br>
&nbsp;&nbsp; CPU_8BIT_XOR(m_RegisterAF.hi, m_RegisterAF.hi, false) ;<br>
&nbsp;&nbsp; return 4;<br><br>

&nbsp;&nbsp; case 0x20 : <br>
&nbsp;&nbsp; CPU_JUMP_IMMEDIATE( true, FLAG_Z, false ) ;<br>
&nbsp;&nbsp; return 8;<br><br>

&nbsp;&nbsp; case 0xCC : <br>
&nbsp;&nbsp; CPU_CALL( true, FLAG_Z, true) ;<br?   ="" return="" 12;<br=""><br>

&nbsp;&nbsp; case 0xD0: <br>
&nbsp;&nbsp; CPU_RETURN( true, FLAG_C, false ) ; <br>
&nbsp;&nbsp; return 8;<br><br>

&nbsp;&nbsp; case 0xCB: <br>
&nbsp;&nbsp; return ExecuteExtendedOpcode( ) ;  <br><br>

&nbsp;&nbsp; default: <br>
&nbsp;&nbsp; assert(false); return 0 ;// unhandled <b style="color:black;background-color:#99ff99">opcode</b><br?   ="" }<br="">
}

</br?></br?></p>

The <b style="color:black;background-color:#99ff99">opcode</b> 0xCB is the extended <b style="color:black;background-color:#99ff99">opcode</b> meaning that whenever we in encounter the 0xCB <b style="color:black;background-color:#99ff99">opcode</b> we have to decode the next immediate byte in memory and treat that as the <b style="color:black;background-color:#99ff99">opcode</b>.
 The opcodes 0xAB is different
to 0xCB 0xAB. This means we can have more opcodes then the standard 
0x0-0xFF. ExecuteExtendedOpcode function is identical layout to 
ExecuteOpcode except it deals with the extended opcodes like so:

<p class="code">
int Emulator::ExecuteExtendedOpcode( ) <br>
{<br>
&nbsp;&nbsp; BYTE <b style="color:black;background-color:#99ff99">opcode</b> = ReadMemory(m_ProgramCounter) ; <br>
&nbsp;&nbsp; m_ProgramCounter++; <br><br>

&nbsp;&nbsp; switch(<b style="color:black;background-color:#99ff99">opcode</b>)<br>
&nbsp;&nbsp; { <br>
&nbsp;&nbsp; case 0xB : <br>
&nbsp;&nbsp; CPU_RRC(m_RegisterDE.lo) ;<br>
&nbsp;&nbsp; return 8; <br><br>

&nbsp;&nbsp; case 0x73 : <br>
&nbsp;&nbsp; CPU_TEST_BIT( m_RegisterDE.lo, 6 ) ;<br>
&nbsp;&nbsp; return 8;<br><br>

&nbsp;&nbsp; default: <br>
&nbsp;&nbsp; assert(false); return 0; // unhandled extended <b style="color:black;background-color:#99ff99">opcode</b><br>
&nbsp;&nbsp; }<br? }="" <="" p="">

Thats all there is to emulating the Z80 except of course actually 
implementing all the functions that are called from the switch statement
 :-).

Remember that all the CPU instructions you have to implement can be 
found in the <a href="http://www.codeslinger.co.uk/pages/projects/gameboy/files/GB.pdf"><b style="color:black;background-color:#a0ffff">Gameboy</b> PDF</a> im hosting.



</br?></p>

<h3><b>8-Bit Loads:</b></h3>
<p>

The 8-Bit Loads put one byte of immediate data from memory into one of 
the 8 registers. So if the function CPU_8IT_LOAD takes a reference of 
one of the registers as an argument like <b style="color:black;background-color:#99ff99">opcode</b> 0x06 then it can
be used for all 8 registers. <b style="color:black;background-color:#99ff99">Opcode</b> 0x06 loads one byte of immediate data into register B. Implementation:

</p><p class="code">
void Emulator::CPU_8BIT_LOAD( BYTE&amp; reg )<br>
{<br>
&nbsp;&nbsp; BYTE n = ReadMemory(m_ProgramCounter) ;<br>
&nbsp;&nbsp; m_ProgramCounter++ ;<br>
&nbsp;&nbsp; reg = n ;<br>
}
</p>

This function can be used for all 8 registers, not just B.

<p></p>

<h3><b>8-Bit ADD:</b></h3>
<p>

This function adds one byte to one of the registes and sets flags. Like 
CPU_8BIT_LOAD if we pass a reference as a parameter then it can be used 
for all 8 registes not just register A as in the example (<b style="color:black;background-color:#99ff99">opcode</b> 0x80). 
This function takes a total of 4 arguments, these are:

</p><p class="block">

Argument 1: The register reference where the ADD result will be placed <br>
Argument 2: The byte to add to the register if it is not immediate data<br>
Argument 3: Wheter what we want to add to the register is immediate data<br>
Argument 4: Wheter we also want to add the carry flag<br>

</p>

There are two type of z80 8-bit add instructions. The first adds a byte 
to the register and the second one adds a byte plus the carry flag to 
the register (hence the fourth argument). This instruction is called 
ADC.<br><br>

This is the conditions the flags get set under:

<p class="block">
FLAG_Z: If after the addition the result is 0<br>
FLAG_N: Set to 0<br>
FLAG_C: If the result will be greater than max byte (255)<br>
FLAG_H: If there will be a carry from the lower nibble to the upper nibble
</p>

FLAG_C needs to check the result before the addition, otherwise how will
 you know if the result was greater than 255 if when it does go greater 
than 255 it wraps round back to 0?<br><br>

This is the implementation of 8-Bit ADD

<p class="code">
void Emulator::CPU_8BIT_ADD(BYTE&amp; reg, BYTE toAdd, int cycles, bool useImmediate, bool addCarry)<br>
{<br>
&nbsp;&nbsp; BYTE before = reg ;<br>
&nbsp;&nbsp; BYTE adding = 0 ;<br><br>

&nbsp;&nbsp; // are we adding immediate data or the second param?<br>
&nbsp;&nbsp; if (useImmediate)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; BYTE n = ReadMemory(m_ProgramCounter) ;<br>
&nbsp;&nbsp;&nbsp;&nbsp;  m_ProgramCounter++ ;<br>
&nbsp;&nbsp;&nbsp;&nbsp;  adding = n ;<br>
&nbsp;&nbsp; }<br>
&nbsp;&nbsp; else<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; adding = toAdd ;<br>
&nbsp;&nbsp; }<br><br>

&nbsp;&nbsp; // are we also adding the carry flag?<br>
&nbsp;&nbsp; if (addCarry)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; if (TestBit(m_RegisterAF.lo, FLAG_C))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; adding++ ;<br>
&nbsp;&nbsp; }<br><br>

&nbsp;&nbsp; reg+=adding ;<br><br>

&nbsp;&nbsp; // set the flags<br>
&nbsp;&nbsp; m_RegisterAF.lo = 0 ;<br><br>

&nbsp;&nbsp; if (reg == 0)<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_Z) ;<br><br>

&nbsp;&nbsp; WORD htest = (before &amp; 0xF) ;<br>
&nbsp;&nbsp; htest += (adding &amp; 0xF) ;<br><br>

&nbsp;&nbsp; if (htest &gt; 0xF)<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_H) ;<br><br>

&nbsp;&nbsp; if ((before + adding) &gt; 0xFF)<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_C) ;<br>

}
</p>


<p></p>

<h3><b>8-Bit Sub:</b></h3>
<p>

This is pretty much identical to 8-Bit Add except its for subtractions. 
It still can use immediate data or the second argument and it can also 
minus the carry flag aswell. This is how the flags
are affected:

</p><p class="block">
FLAG_Z: If after the subtraction the result is 0<br>
FLAG_N: Set to 1<br>
FLAG_C: If the result will be less than 0<br>
FLAG_H: If there will be a carry from the lower upper nibble to the lower nibble
</p>

It is implemented like so:

<p class="code">
void Emulator::CPU_8BIT_SUB(BYTE&amp; reg, BYTE subtracting, int cycles, bool useImmediate, bool subCarry)<br>
{<br>
&nbsp;&nbsp; BYTE before = reg ;<br>
&nbsp;&nbsp; BYTE toSubtract = 0 ;<br><br>

&nbsp;&nbsp; if (useImmediate)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; BYTE n = ReadMemory(m_ProgramCounter) ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_ProgramCounter++ ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; toSubtract = n ;<br>
&nbsp;&nbsp; }<br>
&nbsp;&nbsp; else<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; toSubtract = subtracting ;<br>
&nbsp;&nbsp; }<br><br>

&nbsp;&nbsp; if (subCarry)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; if (TestBit(m_RegisterAF.lo, FLAG_C))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; toSubtract++ ;<br>
&nbsp;&nbsp; }<br><br>

&nbsp;&nbsp; reg -= toSubtract ;<br><br>

&nbsp;&nbsp; m_RegisterAF.lo = 0 ;<br><br>

&nbsp;&nbsp; if (reg == 0)<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_Z) ;<br><br>

&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_N) ;<br><br>

&nbsp;&nbsp; // set if no borrow<br>
&nbsp;&nbsp; if (before &lt; toSubtract)<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_C) ;<br><br>

&nbsp;&nbsp; SIGNED_WORD htest = (before &amp; 0xF) ;<br>
&nbsp;&nbsp; htest -= (toSubtract &amp; 0xF) ;<br><br>

&nbsp;&nbsp; if (htest &lt; 0)<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_H) ;<br>

}

</p>

<p></p>

<h3><b>8-Bit XOR:</b></h3>
<p>

All the logical operators AND, OR and XOR work the same as ADD and SUB, 
except it doesnt care about the carry flag (so there is no fourth 
argument). The flags are set like so for XOR:

</p><p class="block">

FLAG_Z: If the result is 0<br>
FLAG_N: Set to 0<br>
FLAG_C: Set to 0<br>
FLAG_H: Set to 0

</p>

Nice and easy. This is the implementation:

<p class="code">
void Emulator::CPU_8BIT_XOR(BYTE&amp; reg, BYTE toXOr, int cycles, bool useImmediate)<br>
{<br>
&nbsp;&nbsp; BYTE myxor = 0 ;<br><br>

&nbsp;&nbsp; if (useImmediate)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; BYTE n = ReadMemory(m_ProgramCounter) ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_ProgramCounter++ ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; myxor = n ;<br>
&nbsp;&nbsp; }<br>
&nbsp;&nbsp; else<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;  myxor = toXOr ;<br>
&nbsp;&nbsp; }<br><br>

&nbsp;&nbsp; reg ^= myxor ;<br><br>

&nbsp;&nbsp; m_RegisterAF.lo = 0 ;<br><br>

&nbsp;&nbsp; if (reg == 0)<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_Z) ;<br>
}
</p>

<p></p>

<h3><b>Jumps:</b></h3>
<p>

There are a fair few jump instructions but they all do the same thing. 
Jumps to a specific address, however most of the jump instruction only 
jumps if a certain condition is true. Some jump instructions
jumps to the address pointed to by the next two bytes of the immediate 
data. Where the other jump instructions jumps to the current program 
counter + the next immediate data (called
Jump Immediate). One
jump instructions jumps to the address pointed to by register HL. This 
is the implementation of the JUMP_IMMEDIATE instruction:

</p><p class="code">
void Emulator::CPU_JUMP_IMMEDIATE(bool useCondition, int flag, bool condition)<br>
{<br>
&nbsp;&nbsp; SIGNED_BYTE n = (SIGNED_BYTE)ReadMemory(m_ProgramCounter) ;<br><br>

&nbsp;&nbsp; if (!useCondition)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_ProgramCounter += n;<br>
&nbsp;&nbsp; }<br>
&nbsp;&nbsp; else if (TestBit(m_RegisterAF.lo, flag) == condition)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_ProgramCounter += n ;<br>
&nbsp;&nbsp; }<br><br>

&nbsp;&nbsp; m_ProgramCounter++ ;<br>
}
</p> 

As you can see if we are using a jump condition (i.e. jump if the Z flag
 is set) then we jump to address of 
m_ProgramCounter+ReadMemory(m_ProgramCounter).

<p></p>

<h3><b>Calls:</b></h3>
<p>
The call instruction is the same as the Jump instruction which jumps to 
the address of the next two immediate data. However the difference is 
that a call is a subroutine and will eventually
return to its current address, where as a jump doesn't. So in order to 
know where to return to we have to push the current program counter on 
to the stack.

</p><p class="code">
void Emulator::CPU_CALL(bool useCondition, int flag, bool condition)<br>
{<br>
&nbsp;&nbsp; WORD nn = ReadWord( ) ;<br>
&nbsp;&nbsp; m_ProgramCounter += 2;<br><br>

&nbsp;&nbsp; if (!useCondition)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; PushWordOntoStack(m_ProgramCounter) ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_ProgramCounter = nn ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; return ;<br>
&nbsp;&nbsp; }<br><br>

&nbsp;&nbsp; if (TestBit(m_RegisterAF.lo, flag)==condition)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; PushWordOntoStack(m_ProgramCounter) ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_ProgramCounter = nn ;<br>
&nbsp;&nbsp; }<br>
}
</p>

Remember this is the same as the other Jump instruction (not jump 
immediate) except that the jump instruction doesnt push anything onto 
the stack

<p></p>

<h3><b>Returns:</b></h3>
<p>
Now we've handled calls to subroutines we neeed to return from them. The
 way you return is you pop a word from the stack and assign it to the 
program counter (remember you pushed the program
counter onto the stack with the call function). Once again we can put a 
condition on the return aswell.

</p><p class="code">
void Emulator::CPU_RETURN(bool useCondition, int flag, bool condition)<br>
{<br>
&nbsp;&nbsp; if (!useCondition)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_ProgramCounter = PopWordOffStack( ) ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; return ;<br>
&nbsp;&nbsp; }<br><br>

&nbsp;&nbsp; if (TestBit(m_RegisterAF.lo, flag) == condition)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_ProgramCounter = PopWordOffStack( ) ;<br>
&nbsp;&nbsp; }<br>
}
</p>


<p></p>

<h3><b>Extended: Rotate Right Through Carry:</b></h3>
<p>

This instruction rotates a regiser A right and sets the carry flag with 
the least significant bit before the rotation. A right rotation means 
everything gets shifted right one and the
most significant bit gets set to what the least significant bit was 
before the rotation.  All the flags get set to 0 except the carry flag 
which as previously mentioned gets set
to the least significant bit of reg a before the rotation

</p><p class="code">
void Emulator::CPU_RRC(BYTE&amp; reg)<br>
{<br>
&nbsp;&nbsp; bool isLSBSet = TestBit(reg, 0) ;<br><br>

&nbsp;&nbsp; m_RegisterAF.lo = 0 ;<br><br>

&nbsp;&nbsp; reg &gt;&gt;= 1;<br><br>

&nbsp;&nbsp; if (isLSBSet)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_C) ;<br>
&nbsp;&nbsp;&nbsp;&nbsp; reg = BitSet(reg,7) ;<br>
&nbsp;&nbsp; }<br>

&nbsp;&nbsp; if (reg == 0)<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_Z) ;<br>
}
</p>

<p></p>

<h3><b>Extended: TestBit:</b></h3>
<p>
This tests the bit of a byte and sets the approprite flags. The flags are set like so:

</p><p class="block">
FLAG Z: Set if the bit is 0<br>
FLAG_N: Set to 0<br>
FLAG_C: Unchanged<br>
FLAG_H: Set to 1
</p>

This is the implemenation:

<p class="code">
void Emulator::CPU_TEST_BIT(BYTE reg, int bit, int cycles)<br>
{<br>
&nbsp;&nbsp; if (TestBit(reg, bit))<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitReset(m_RegisterAF.lo, FLAG_Z) ;<br>
&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_Z) ;<br><br>

&nbsp;&nbsp; m_RegisterAF.lo = BitReset(m_RegisterAF.lo, FLAG_N) ;<br>
&nbsp;&nbsp; m_RegisterAF.lo = BitSet(m_RegisterAF.lo, FLAG_H) ;<br>

}
</p>

The important thing to know with the TEST_BIT instruction (and with the 
SET_BIT and RESET_BIT instructions). Is that the documentation states 
that extended <b style="color:black;background-color:#99ff99">opcode</b>
 0x40 tests bit 0 of register B.
But doesnt give the opcodes for when bit 1 is needed to be tested or bit
 2. Luckily all you have to do is add 0x8 to get hex value of the other 
opcodes. So to test bit 1 of register B it is 0x48. To
test bit 2 of register B it is 0x50. This also works for the other 
registers, for example the documentation says that <b style="color:black;background-color:#99ff99">opcode</b>
 0x41 tests bit 0 of register C. From this we can also deduce that 0x49
tests bit 1 of register C etc. You need to emulate all these instruction
 to test all 7 bits of all 8 registers. The same goes for emulating the 
SET_BIT and RESET_BIT functions.

<br><br>

<p></p>

</div>

<div id="sidebar">
<h3><b style="color:black;background-color:#a0ffff">Gameboy</b> Emulation:</h3>

<p>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/beginning.html">Getting Started</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/hardware.html">The Hardware</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/memory.html">Memory Control and Map</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/banking.html">ROM and RAM Banking</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/timers.html">The Timers</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/interupts.html">Interupts</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/lcd.html">LCD</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/dma.html">DMA Transfer</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/graphics.html">Graphic Emulation</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/joypad.html">Joypad Emulation</a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/opcodes.html"><b style="color:black;background-color:#99ff99">Opcode</b> <b style="color:black;background-color:#ff9999">Examples</b></a><span class="hide"> | </span>
<a class="sidelink" href="http://www.codeslinger.co.uk/pages/projects/gameboy/finished.html">Finished Product</a><span class="hide"> | </span>

</p>

</div>
    
<div id="footer">
Copyright © 2008 <b style="color:black;background-color:#ffff66">codeslinger</b>.co.uk
</div>

</div>




</div></body></html>